<?php
namespace app\admin\controller;
use app\admin\controller\Auth;
use think\Db;
use think\Config;
class Partners extends Auth{
	// 渲染添加合作伙伴模板
	public function addPartners(){
		return $this->fetch();
	}
	// 执行添加合作伙伴
	public function doAddPartners(){
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('image');
		if(empty($file)){
			$this->error('请选择logo图片');
			exit;
		}
		// 移动到框架应用根目录/public/uploads/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			$this->error($file->getError());
			exit;
		}
		$data['createtime'] = time();
		$result = Db::name('partners')->insert($data);
		if($result){
			$this->redirect('/admin/partners/addPartners');
		}else{
			$this->error('添加失败');
		}
	}
	// 合作伙伴列表
	public function partnersList(){
		$list = Db::name('partners')->paginate(10);
		$this->assign('list',$list);
		return $this->fetch();
	}
	// 删除合作伙伴
	public function delPartners(){
		$id = input('pid');
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$id);
		$result = Db::name('partners')->where($where)->delete();
		if($result){
			echo json_encode(['code' => 0,'msg' => '删除成功']);
		}else{
			echo json_encode(['code' => 1,'msg' => '删除失败']);
		}
	}
}